/**
 * node 操作mysql js
 * @type {*}
 */
var mysql = require("mysql");

/**
 *  mysql pool
 * @type {*}
 */
var pool = mysql.createPool({
    host:"127.0.0.1",
    user:'root',
    password:'',
    database:'mychat',
    port:'3306'
});

/**
 *  用户信息查询  支持全部查询和单个查询 单个查询只支持id查询
 * @param msg
 */
exports.getUserinfo = function(msg){
    var id = {};
    var users = new Array();
    if(undefined == msg || null==msg){
        id="null";
    }else{
        id = msg;
    }

    var sql ="select * from userinfo where 1=1 ";
    if(null!=id){
         sql +=" id= ? ";
    }
    pool.query(sql,[msg],function(err,rows){
        if(err){
            throw err;
        }

        //若有值则返回数值
        for(var i =0;i<rows.length;i++){
            users.push(rows[i]);
        }
        return users;
    });
    console.log(msg);
}

/**
 * 用户登陆信息验证
 *
 * @param id
 * @param username 用户姓名
 * @param pwd   用户密码
 * @returns {boolean} 返回值 ture 验证正确 false 验证错误
 */
exports.isUser = function(id,username,pwd){
    var users = getUserinfo(id);
    if(users.length<1){
        return false;
    }
    var user = user[0];
    if(username==user.loginname && pwd == user.pwd){
        return true;
    }else{
        return false;
    }

}

/**
 * 往数据库中新增数据
 * @param name 登陆名称
 * @param pwd   登陆密码
 * @param nickname  昵称
 * @returns {boolean}
 */
exports.addUserInfo=function(name,pwd,nickname){
    if(name ==null || pwd ==null || nickname == null){
        return false;
    }

    var sql = "insert into userinfo(loginname,pwd,nickname) values (?,?,?)";
    

    return false;
}